package su.mvc.action.admin;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import su.mvc.action.ActionSupport;
import su.mvc.model.MenuItem;
import su.mvc.service.UserService;
import su.mvc.service.gui.MenuService;

import java.util.Map;
import java.util.logging.Logger;

@Results(value = {
        @Result(name=ActionSupport.SUCCESS, location="menu-list.jsp"),
        @Result(name=ActionSupport.INPUT, location="menu-create-edit.jsp") ,
        @Result(name = ActionSupport.REDIRECT_CANCEL, type = "redirect", location = "/admin/menu"),
        @Result(name = ActionSupport.REDIRECT_OK, type = "redirect", location = "/admin/menu", params = {"messages", "menu.saved.successfully"}),
        @Result(name = ActionSupport.ACCESS_DENIED, location = "access-denied.jsp")
})
public class MenuAction extends ActionSupport {

    private MenuService menuService;

    private UserService userService;

    private Map<String, String> menuItems;

    private Boolean edit = false;

    private MenuItem item;

    private static final Logger log = Logger.getLogger(MenuAction.class.getName());


    public String execute() throws Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        menuItems = menuService.getMenuItemsMap("&nbsp;");
        return SUCCESS;
    }

    public String edit() throws Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        setEdit(true);
        item = menuService.getMenuItemByKey(item.getKey());
        menuItems = menuService.getMenuItemsMap("-");
        menuItems.remove(item.getKey().getString());
        return INPUT;
    }
    
    public String delete() throws Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        menuService.removeMenuItem(item.getKey());
        return REDIRECT_OK;
    }

    public String cancel() throws Exception{
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        return REDIRECT_CANCEL;
    }

    public String create() throws Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        setEdit(false);
        menuItems = menuService.getMenuItemsMap("-");
        item = new MenuItem();
        return INPUT;
    }

    public String save() throws  Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        if (validateForm()) {
            menuService.saveMenuItem(item);
            return REDIRECT_OK;
        }
        menuItems = menuService.getMenuItemsMap("-");
        return INPUT;
    }

    public boolean validateForm() {
        if (StringUtils.isEmpty(item.getName())) addError("error.menu.name.empty");
        return getActionErrors().isEmpty();
    }
    
    public MenuItem getTopMenuItem() {
        return menuService.getTOP();
    }

    public MenuService getMenuService() {
        return menuService;
    }

    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }

    public Map<String, String> getMenuItems() {
        return menuItems;
    }

    public void setMenuItems(Map<String, String> menuItems) {
        this.menuItems = menuItems;
    }

    public Boolean getEdit() {
        return edit;
    }

    public void setEdit(Boolean edit) {
        this.edit = edit;
    }

    public MenuItem getItem() {
        return item;
    }

    public void setItem(MenuItem item) {
        this.item = item;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
